package cn.sys.controller;

import cn.sys.controller.dto.SearchDTO;
import cn.sys.service.ManagerInfoService;
import cn.sys.service.impl.ManagerInfoServiceImpl;
import cn.sys.util.FileUploadUtil;
import cn.sys.util.ParseRequestParameterUtil;
import cn.sys.util.ResponseUtil;
import cn.sys.util.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/managerInfo")
public class ManagerInfoServlet extends HttpServlet {
    private ManagerInfoService managerInfoService = new ManagerInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        System.out.println(action);

        switch (action){
            case "login":login(req, resp);break;
            case "addStudent":addStudent(req, resp);break;
            case "quitDormitory":quitDormitory(req, resp);break;
            case "addStudentExcel" : addStudentExcel(req,resp);break;
            case "getStudentAndDormitoryList":getStudentAndDormitoryList(req, resp);break;
            case "findStudentByStuIdAndPhoneNum" : findStudentByStuIdAndPhoneNum(req, resp);break;
            case "dealStudentLocked" : dealStudentLocked(req, resp);break;
        }
    }

    // 根据学生的id和手机号查询学生信息
    private void findStudentByStuIdAndPhoneNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = (SearchDTO) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), SearchDTO.class);
        ResultData resultData = managerInfoService.findStudentByStuIdAndPhoneNum(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    // 学员账号解冻
    private void dealStudentLocked(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 因为是从查询列表得到的数据，不必再判断是否存在
        Integer stuId = Integer.valueOf(req.getParameter("stuId"));

        // 需要：修改锁定状态、失败的登录次数、重置密码
        ResultData resultData = managerInfoService.dealLockedState(stuId);
        ResponseUtil.send(resp, resultData);
    }

    // 查询学生列表，以及对应的宿舍信息、宿舍位置信息
    private void getStudentAndDormitoryList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = (SearchDTO) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), SearchDTO.class);
        ResultData resultData = managerInfoService.findStudentList(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 管理员选择表格文件，批量添加学生信息到数据库中
     * @param req
     * @param resp
     */
    private void addStudentExcel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Map<String, Object>> students = FileUploadUtil.getMapInfoFromExcel(req, "excelFile");
        //System.out.println(students);
        ResultData resultData = managerInfoService.addStudentList(students);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 管理员对学员的退寝办理功能
     * @param req
     * @param resp
     */
    private void quitDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer stuId = Integer.valueOf(req.getParameter("stuId"));
        ResultData resultData = managerInfoService.quitDormitory(stuId);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 添加一名学生信息
     * @param req
     * @param resp
     */
    private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String stuName = req.getParameter("stuName");
        String realName = req.getParameter("realName");
        Integer age = Integer.valueOf(req.getParameter("age"));
        String gender = req.getParameter("gender");
        String phoneNum = req.getParameter("phoneNum");
        String schoolName = req.getParameter("schoolName");
        ResultData resultData = managerInfoService.addStudent(stuName, realName, age, gender, phoneNum, schoolName);
        ResponseUtil.send(resp, resultData);
    }

    /**
     * 登录
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String account = req.getParameter("studentName");
        String password = req.getParameter("password");
        ResultData resultData = managerInfoService.login(account, password);
        //如果管理员登录成功，则需要将管理员信息存入到session中
        if ( (Integer) resultData.get("code") == ResultData.LOGIN_SUCCESS) {
            HttpSession session = req.getSession();
            session.setAttribute("managerInfo", resultData.get("data"));
        }
        ResponseUtil.send(resp, resultData);
    }
}
